#!/usr/bin/perl

use strict;
use warnings;
use lib "./libs";
use lib "./app/Commands";

use JSON;
use DBI;
use Data::Dump qw(dump);

use Const ();
use Utils ();
use App ();
use Migration ();
use MigrationFen ();
use Ddl ();
use Data ();

my $version = "2.2.8";

sub main
{
    my $param = shift @ARGV;
    unless (defined($param)) {
        print "invalid command";
        exit 1;
    }
    my @param_arr = split /:/, $param;
    my $command = $param_arr[0];
    my $action = $param_arr[1];

    if ($command eq "version") {
        print "version: $version\n";   
    }
    # 数据对比
    elsif ($command eq "migration" && $action eq "list") {
        Migration::list(@ARGV);
    } 
    elsif ($command eq "migration" && $action eq "run") {
        Migration::run(@ARGV);
    }
    elsif ($command eq "migration" && $action eq "archive") {
        Migration::archive(@ARGV);
    }
    elsif ($command eq "migration" && $action eq "gen-main") {
        Migration::genMain(@ARGV);
    }

    elsif ($command eq "migration-fen" && $action eq "conn-list") {
        MigrationFen::connList(@ARGV);
    }
    elsif ($command eq "migration-fen" && $action eq "list") {
        MigrationFen::list(@ARGV);
    }
    elsif ($command eq "migration-fen" && $action eq "run") {
        MigrationFen::run(@ARGV);
    }
    elsif ($command eq "migration-fen" && $action eq "gen-main") {
        MigrationFen::genMain(@ARGV);
    }
    elsif ($command eq "migration-fen" && $action eq "gen-main2") {
        MigrationFen::genMain2(@ARGV);
    }

    # sql迁移
    elsif ($command eq "ddl" && $action eq "export") {
        # 检查是否有额外的参数（输入文件路径）
        my $input_file = $ARGV[0];
        Ddl::export($input_file);
    }
    elsif ($command eq "ddl" && $action eq "check") {
        Ddl::check();
    }
    elsif ($command eq "ddl" && $action eq "create") {
        my $ddl_path = $ARGV[0];
        Ddl::create($ddl_path);
    }

    elsif ($command eq "data" && $action eq "conn-list") {
        Data::connList(@ARGV);
    }
    elsif ($command eq "data" && $action eq "run") {
        Data::run(@ARGV);
    }
}

main();

1;

__END__